#!/usr/bin/env ruby

require "Lexer"

if ((ARGV[0] <=> "-e") == 0) then    # Is a string?
    arg = ARGV[1]
else                                 # Then, is a file
    arg = File.open(ARGV[0], "r")
end

# Creates a new lexer
lexer = Lexer.new(arg)
ok = Array.new         # Tokens are here
notok = Array.new      # Errors are here


# Starts the recognition of tokens
begin
    begin
        tok = lexer.yylex
        ok << tok if tok != nil
    rescue Exception => e
        notok << e
    end
end while tok != nil

# Print the messages
if notok.length > 0
    final = notok
else
    final = ok
end

for i in 0...final.length
    puts final[i]
end

# Finally, if it was a file, we close it
if ((ARGV[0] <=> "-e") != 0) then
    arg.close
end